﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Commonality.Model;
using PLCControl;
using SerialPortModel;
using ScanningGunModel;
using Separate.DAL.MySQLFunction;
using TextEditing;

namespace WindowsFormsApp
{
    public partial class Form1 : Form
    {
        private ImplementLADEx PlcLoop;
        private DataReceived readData;

        CKeyenceScanningGunModel KeyenceScanningGunModel;


        public TPLCCell m_xInput1;
        public TPLCCell m_yOutput11;
        public TPLCCell time0;
        public TPLCCell time1;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            InitPlcCell();
            InitAuto();


        }
        private void InitPlcCell()
        {
            //输入
            CPLCControl.InitPlcCell(out m_xInput1, ESignalType.SIG_X, 1, 0, 1);

            //输出
            CPLCControl.InitPlcCell(out m_yOutput11, ESignalType.SIG_Y, 1, 0, 11);

            //定时器
            CPLCControl.InitPlcCell(out time0, ESignalType.SIG_T, 0, 0);
            CPLCControl.InitPlcCell(out time1, ESignalType.SIG_T, 0, 1);
        }
        private void InitAuto()
        {
            CPLCControl.workstationFirstAddress = 5;
            CPLCControl.workstationNumOffset = 1;
            PlcLoop = new ImplementLADEx(PlcImplement);
            CPLCControl.SetLADProcessEx(PlcLoop);


            com11 = new CSerialPortModel("COM11", 9600, 8, System.IO.Ports.Parity.Odd, System.IO.Ports.StopBits.One);
            readData = new DataReceived(Read);
            com11.SetDataReceived(readData);


            KeyenceScanningGunModel = new CKeyenceScanningGunModel("COM21", 115200, 8, Parity.None, StopBits.One, QRcode);
        }
        private void QRcode(ref string sn)
        {
            label1.Text = sn;
        }
        public void PlcImplement()
        {
            if (CPLCControl.LAD_CheckCell(time0) == 1)
            {
                this.Text = "100ms";
                CPLCControl.LAD_ResetCell(time0);
                CPLCControl.LAD_SetTCell(time1, 10);
            }
            if (CPLCControl.LAD_CheckCell(time1) == 1)
            {
                this.Text = "0ms";
                CPLCControl.LAD_ResetCell(time1);
                CPLCControl.LAD_SetTCell(time0, 10);
            }
        }
        CSerialPortModel com11;
        private void Button1_Click(object sender, EventArgs e)
        {
            CPLCControl.LAD_SetTCell(time0, 100);
            com11.Open();

        }

        private void Button2_Click(object sender, EventArgs e)
        {
            Byte[] TxData = { 1, 2, 3, 4, 5, 6, 7, 8 };
            com11.SendData(TxData);
        }
        
        private void Read(object sender, SerialDataReceivedEventArgs e)
        {

        }

        private void Button3_Click(object sender, EventArgs e)
        {
            KeyenceScanningGunModel.ScanCodeTimeout(7000);
        }

        private void Button4_Click(object sender, EventArgs e)
        {
            int a = 0;
            string b = "";
            MySqlFunction.PackageOrientation.PackageOrientation_SELECT("ABCDEFGHIJKLMNOPR",ref a,ref b);

            label1.Text = a.ToString() + b;
        }

        private void Button5_Click(object sender, EventArgs e)
        {
            string usn = CTextCreate.GetRandomString(17, true, false, true, false, "");
            MySqlFunction.PackageOrientation.PackageOrientation_INSERT(usn, 0, "OK");
        }

        private void Button6_Click(object sender, EventArgs e)
        {
            MySqlFunction.PackageOrientation.PackageOrientation_UPDATE("ABCDEFGHIJKLMNOPR", 0, "Liebeskind");
        }
    }
}
